//package me.zhengjie.timing;
//
//import lombok.RequiredArgsConstructor;
//import me.zhengjie.agvSend.AgvSendService;
//import me.zhengjie.pojo.dto.AGVTaskContinueExecuteDto;
//import me.zhengjie.pojo.dto.ContinueExecute;
//import me.zhengjie.pojo.entity.IwsSOutside;
//import me.zhengjie.pojo.entity.WcsInboundMission;
//import me.zhengjie.sqlservice.IwsSOutsideService;
//import me.zhengjie.sqlservice.IwsWareHouseService;
//import me.zhengjie.sqlservice.WcsInboundMissionService;
//import me.zhengjie.tcp.CircleLibraryTCP;
//import me.zhengjie.util.state.MaterialStatus;
//import me.zhengjie.util.state.TaskType;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//
//@Component
//@RequiredArgsConstructor
//public class CircleLibraryTiming {
//
//    private static final Logger chainLine = LoggerFactory.getLogger("chainLine");
//
//    private final IwsWareHouseService iwsWareHouseService;
//
//    private final CircleLibraryTCP circleLibraryTCP;
//
//    private final AgvSendService agvSendService;
//
//    private final WcsInboundMissionService wcsInboundMissionService;
//
//    private final IwsSOutsideService iwsSOutsideService;
//
//
//    @Transactional
//    @Scheduled(fixedDelay = 1000)
//    public void circleLibrary(){
//        //查询1楼回库有任务
////        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectContainerTypeAndInboundType(TaskType.PALLET.getStatus(),TaskType.circle_library_sorting.getStatus());
////        if (!wcsInboundMissionList.isEmpty()){
//            //查询储位是否有货
////        IwsWareHouse iwsWareHouse = iwsWareHouseService.selectStorageCode("回库测高储位码");
//            IwsSOutside iwsSOutsides = iwsSOutsideService.selectCircleLibrary(TaskType.STORAGE_TYPE_ONE_FLOOR_CIRCLE_LIBRARY.getMessage(), 3);
//            if (iwsSOutsides != null){
//                String id = iwsSOutsides.getId();
//                //todo 储位上有货，通知PLC开始测高(通知PLD开始测高在AGV上报到达中间点)
//                boolean b = circleLibraryTCP.writeWithRetry(510, 1, MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus(), 3);
//                //通知PLC到达储位
//                boolean b3 = circleLibraryTCP.writeWithRetry(00, 1, MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus(), 3);
//                if (b){
//                    //获取读码状态
//                    Integer i = circleLibraryTCP.readHoldingRegisters(11,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus());
//                    if (i == 1){
//                        //获取读码结果
//                        String palletCode = circleLibraryTCP.readHoldingRegistersString(12, MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus());
//                        WcsInboundMission wcsInboundMission = wcsInboundMissionService.selectContainerCode(palletCode);
//                        if (wcsInboundMission != null){
//                            //获取读码信息，中间表嵌套，查询数据是否一致、
//                            //todo 未找到嵌套信息
//                            if (!true){
//                                circleLibraryTCP.writeWithRetry(00, 2,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus(), 3);
//                            }
//                            //todo 嵌套信息不一致，直接报警 发送载具id获取储位信息
//                            if (!true){
//                                circleLibraryTCP.writeWithRetry(00, 3,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus(), 3);
//                            }
//                            //检测高度
//                            Integer tall = circleLibraryTCP.readHoldingRegisters(00,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus());
//                            if (tall == 2){
//                                //测高完成，获取测高结果
//                                Integer tallResult = circleLibraryTCP.readHoldingRegisters(22,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus());
//                                if (tallResult != 0 && tallResult !=5){
//                                    //通过扫码器拿到栈板号，找入库任务下发给AGV继续入库
//                                    AGVTaskContinueExecuteDto agvTaskContinueExecuteDto = new AGVTaskContinueExecuteDto();
//                                    agvTaskContinueExecuteDto.setRobotTaskCode(wcsInboundMission.getInboundCode());
//                                    agvTaskContinueExecuteDto.setTriggerType(wcsInboundMission.getInboundCode());
//                                    //todo
//                                    agvTaskContinueExecuteDto.setTriggerCode("对应的触发编");
//                                    ContinueExecute continueExecute = new ContinueExecute();
//                                    continueExecute.setSeq(1);
//                                    continueExecute.setType("SITE");
//                                    continueExecute.setCode(wcsInboundMission.getDestination());
//                                    boolean b1 = agvSendService.taskContinueExecute(agvTaskContinueExecuteDto);
//                                    if (b1){
//                                        //释放测高储位
////                                    boolean b2 = iwsWareHouseService.updateStorage(iwsWareHouse.getStoreId(),0);
//                                        boolean b2 = iwsSOutsideService.updateStatus(iwsSOutsides.getId(), null, 0);
//                                        if (!b2){
//                                            chainLine.error("回库释放储位失败，数据库异常");
//                                            throw new RuntimeException("回库释放储位失败，数据库异常");
//                                        }
//                                    }
//                                }else if (tallResult == 5){
//                                    //超高释放储位
//                                    boolean b1 = iwsSOutsideService.updateStatus(id, null, 0);
//                                }
//                            }else if (tall > 2){
//                                //测高状态异常，释放储位
//                                boolean b1 = iwsSOutsideService.updateStatus(id, null, 0);
//                            }
//                        }else {
//                            //返回失败
//                            circleLibraryTCP.writeWithRetry(00, 4,  MaterialStatus.ONE_FLOOR_RETURN_TO_THE_WAREHOUSE.getStatus(), 3);
//                        }
//                    }else if (i ==2 || i ==3){
//                        //释放储位
//                        boolean b1 = iwsSOutsideService.updateStatus(id, null, 0);
//                    }
//                }
//
//            }
//    }
//}
